import { reactive, ref } from "vue";
import { getListApi as warehosueManagementApi } from "@/api/managementCenter/warehosueManagement";
export default function InitHook() {
  const formState = reactive<{ warehouseId: string }>({
    warehouseId: ""
  });

  const tableData = ref<StockManagement.XlsxTableData[]>([]);
  const getWarehouseOpts = ref<{ label: string; value: string }>();
  const formDataRef = ref<any>(null);
  const loading = ref<boolean>(false);
  const conifg = {
    beforeChange: (changes: any) => {
      // [[row, prop, oldVal, newVal], ...]
      for (let i = 0; i < changes.length; i++) {
        const target = changes[i];
        if (["pickingBoxWeight", "pickingBoxLength", "pickingBoxWidth", "pickingBoxHeight"].includes(target[1])) {
          if (changes[i][3].length > 5) {
            changes[i][3] = changes[i][3].slice(0, 5);
          }
          if (isNaN(Number(target[3]))) {
            changes[i][3] = 0;
          } else {
            changes[i][3] = Number(changes[i][3]).toFixed(2);
          }
        }
      }
    }
  };
  //获取仓库
  const getWareHouseHandle = () => {
    return warehosueManagementApi({ status: 1, pageNum: 1, pageSize: 9999999, total: 0, userPickWarehouse: 1 }).then((res: any) => {
      const { code, data } = res;
      if (code === 200) {
        getWarehouseOpts.value = data || [];
      }
    });
  };
  return {
    formState,
    conifg,
    loading,
    tableData,
    getWareHouseHandle,
    getWarehouseOpts,
    formDataRef
  };
}
